#define  _CRT_SECURE_NO_WARNINGS

#include "MyStack.h"

void STInit(ST* pst)
{
	assert(pst);

	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

void STPush(ST* pst, MySTDataType x)
{
	if (pst -> top == pst->capacity)
	{

		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		MySTDataType* newnode = (MySTDataType*)realloc(pst->a, newcapacity * sizeof(MySTDataType));
		if (newnode == NULL)
		{
			perror("realloc fail\n");
			return;
		}
		pst->a = newnode;
		pst->capacity = newcapacity;

	}
	pst->a[pst->top] = x;
	pst->top++;
}

MySTDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	return pst->a[pst->top-1];
}

void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	pst->top--;
}

bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

int STSize(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	return pst->top;
}